# -*- coding: utf-8 -*-
# Copyright Š 2010 alexliyu
#
# This file is part of cntop-news.
#
# cntop-news is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cntop-news is base on Top-news-buzz ,
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cntop-news.  If not, see <http://www.gnu.org/licenses/>.

import re

from topnewsbuzz.controller import MainPage
from topnewsbuzz.utils import DateHelper
from topnewsbuzz.news import NewsManager

class NewsListPage(MainPage):
    
    def service(self):
        date = self.__get_date_helper_from_url()
        
        page = 1
        match = re.match('.*(/page/(\d+))', self.request.path)
        if match is not None and match.group(2) is not None:
            page = int(match.group(2))
        self.urls.set_page(page)
            
        
        news_articles = NewsManager.get_top_news(date, page) 
            
        self.template_values.update({'is_article' : False,
                                     'is_list' : True,
                                     'is_home' : True if self.request.path == '/' else False,
                                     'date' : date,
                                     'page' : page,
                                     'news_articles' : news_articles})

    def __get_date_helper_from_url(self):
        match = re.match('/(\d+)/week/(\d+)(/page/(\d+))?', self.request.path)
        if match is not None:
            year = match.group(1)
            week = match.group(2)
            date = DateHelper(year=int(year), week=int(week))
        else:
            match = re.match('/(\d+)/(\d+)/(\d+)(/page/(\d+))?', self.request.path)
            if match is not None:
                year = match.group(1)
                month = match.group(2)
                day = match.group(3)
                date = DateHelper(int(year), int(month), int(day))
            else:
                match = re.match('/(\d+)/(\d+)(/page/(\d+))?', self.request.path)
                if match is not None:
                    year = match.group(1)
                    month = match.group(2)
                    date = DateHelper(int(year), int(month))
                else:
                    match = re.match('/(\d+)(/page/(\d+))?', self.request.path)
                    if match is not None:
                        year = match.group(1)
                        date = DateHelper(int(year))
                    else:                        
                        date = DateHelper()
        return date


class NewsArticlePage(MainPage):
    
    def service(self):
        self.template_url = '../templates/article.html'
        
        match = re.match("/([^/].*)/.*", self.request.path)
        if match is not None:
            id = int(match.group(1))
            news = NewsManager.get_news_by_id(id)
            self.template_values.update({'is_article' : True,
                                         'is_list' : False,
                                         'is_home' : False,
                                         'date' : DateHelper(),
                                         'news' : news})
